package cn.imvc.app.models;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Logging;
import cn.imvc.app.repositories.LoggingRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class LogginModelImpl implements LoggingModel {
	@Autowired
	UserModel userModel;
    @Autowired
    LoggingRepository loggingRepository;
	public void info(Logger logger,String msg) {
		if(MDC.get("uname") == null){
			MDC.put("uname", userModel.getSessionUser().getName());
		}
		logger.info(msg);
	}

	public Page<Logging> find(ViewRequest viewRequest) {
		return loggingRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public Logging get(int id) {
		return loggingRepository.findOne(id);
	}

}
